package com.glq.book.servlet;

import com.glq.book.dao.BookDao;
import com.glq.book.entity.Book;
import com.glq.book.service.BookService;
import com.glq.book.util.IsNullUtil;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/book")
public class BookServlet extends HttpServlet {
    private String type = "";
    private BookService bookService = new BookService();
    private BookDao bookDao = new BookDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            type = "findAll";
        }
        switch (type) {
            case "findAll":
                List<Book> list = bookDao.selectBookList();
                req.getSession().setAttribute("bookList", list);
                resp.sendRedirect("modeBookList.jsp");
                //跳转页面
                break;
            case "userBookList":
                String id = req.getParameter("id");
                Book book = bookDao.selectBookById(Integer.valueOf(id));
                req.getSession().setAttribute("book", book);
                //跳转页面
                resp.sendRedirect("modeBookUpdate.jsp");
                break;
            case "delete":
                id = req.getParameter("id");
                boolean b = bookService.deleteBook(Integer.valueOf(id));
                if (b) {
                    //成功，跳转页面
                    resp.sendRedirect("/book?type=findAll");
                } else {
                    //失败，跳转错误页面
                    resp.getWriter().write("书籍下架失败，请稍后再试");
                }
                break;
            case "getTypeBook":
                String typeName = req.getParameter("typeName");
                list = bookDao.selectBookListByType(typeName);
                req.getSession().setAttribute("bookTypeList", list);
                resp.sendRedirect("webtechnology.jsp");
                break;
            default:
                //跳转错误页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            resp.sendRedirect("/book?type=findAll");
        }
        switch (type) {
            case "add":
                String bookName = IsNullUtil.isEntity(req.getParameter("bookName"));
                String bookPrice = IsNullUtil.isNumEntity(req.getParameter("bookPrice"));
                String bookNumber = IsNullUtil.isNumEntity(req.getParameter("bookNumber"));
                String bookType = IsNullUtil.isEntity(req.getParameter("bookType"));
                String bookFengMian = IsNullUtil.isBookImgEntity(req.getParameter("bookFengMian"));
                String bookAuthor = IsNullUtil.isEntity(req.getParameter("bookAuthor"));
                String bookAbstract = IsNullUtil.isEntity(req.getParameter("bookAbstract"));
                boolean b = bookService.insertBook(bookName, Integer.valueOf(bookPrice), Integer.valueOf(bookNumber), bookType, bookFengMian, bookAuthor, bookAbstract);
                if (b) {
                    //成功，跳转页面
                    resp.sendRedirect("/book?type=findAll");
                } else {
                    //失败，跳转错误页面
                    Gson gson = new Gson();
                    resp.getWriter().write("书籍添加失败,请检查名称");
                }
                break;
            case "update":
                bookName = IsNullUtil.isEntity(req.getParameter("bookName"));
                bookType = IsNullUtil.isEntity(req.getParameter("bookType"));
                bookAuthor = IsNullUtil.isEntity(req.getParameter("bookAuthor"));
                bookAbstract = IsNullUtil.isEntity(req.getParameter("bookAbstract"));
                bookNumber = IsNullUtil.isNumEntity(req.getParameter("bookNumber"));
                bookFengMian = IsNullUtil.isBookImgEntity(req.getParameter("bookFengMian"));
                bookPrice = IsNullUtil.isNumEntity(req.getParameter("bookPrice"));
                String id = req.getParameter("id");
                b = bookService.updateBook(
                        Integer.valueOf(bookPrice),
                        Integer.valueOf(id),
                        bookFengMian,
                        bookName,
                        bookType,
                        bookAuthor,
                        bookAbstract,
                        Integer.valueOf(bookNumber));
                if (b) {
                    //成功，跳转页面
                    resp.sendRedirect("/book?type=findAll");
                } else {
                    //失败，跳转错误页面
                    resp.getWriter().write("更新价格失败，请稍后再试");
                }
                break;
            default:
                //跳转错误页面
        }
    }
}
